<template>
    <div>
        <h2>LOGIN</h2>
        <input type="text" v-model="model.username">
        <input type="text" v-model="model.passwd">

        <button @click="login()">登陆</button>
    </div>
</template>

<script>
export default {
    data() {
        return {
            model:{
                username:"",
                passwd:""
            }
        }
    },
    methods: {
        login(){
            // e.preventDefault();
            this.$store
            .dispatch("login",this.model)
            .then(code => {
                if(code){
                    // 登陆成功重定向
                    const path = this.$route.query.redirect || "/"
                    this.$router.push(path);
                }
            })
            .catch(error =>{
                // 有错误或登录失败
               alert("登录失败")
            })
        }
    },
    
}
</script>